<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<link rel="stylesheet" href="<%=path%>/css/pintuer.css">
<link rel="stylesheet" href="<%=path%>/css/admin.css">
<link rel="shortcut icon" href="<%=path%>/images/login.jfif">
<script src="<%=path%>/js/jquery.js"></script>
<!-- 引入zTree插件 -->
<link rel="stylesheet" type="text/css"
	href="<%=path%>/third/zTree_v3/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
</script>

</head>

<body>
  <div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>增加权限</strong></div>
  <div class="body-content">
    <form id="form-addrole" class="form-x">  
      <input id="fid" name="fid" value="" type="hidden"/>
      <div class="form-group">
        <div class="label">
          <label>权限编号：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" value="" name="menuid"  id="menu_id_input" data-validate=" required:请输入权限编号" />
          <span></span>
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>权限名称：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" value="" name="name" id="menu_name_input"data-validate=" required:请输入行政区域名称" />
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>权限路径：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" value="" name="url" id="url_input"data-validate=" required:请输入行政区域名称" />
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>上一级权限：</label>
        </div>
        <div class="field">
          <input type="hidden" id="menuid" name="menuid" value="" /> 
		    <div>
	           <ul id="zTree" class="ztree"></ul>
	        </div>
          <div class="tips"></div>
        </div>
      </div>
   
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <button id="add_btn" class="button bg-main icon-check-square-o" type="button"  onclick="addMenu()"> 提交</button>
        </div>
      </div>
    </form>
  </div>
</div>


<script type="text/javascript">
            //异步加载树形行政区域信息
 var setting = {
   data: {
                                simpleData: {
                               enable: true,  
                               idKey: "menuid",  
                               pIdKey: "pid",   
                               rootPId: -1  
                                },
                                key: {
 	                              name: "name" 
	                          }
                             },
		check : {
 			enable : true,
			chkStyle: "checkbox",
 		    chkboxType: { "Y": "", "N": "s" }
 		},

 		async : {
 			type : "post",
			enable : true,
			url : "<%=path%>/menu/menudata_load",  
 		}
	};
 	$(document).ready(function() {
		$.fn.zTree.init($("#zTree"), setting);
	});
 //校验行政区域编号是否重复
	$("#menu_id_input").change(function() {
		var menuid = this.value;
		$.ajax({
		          url:"<%=path%>/menu/menu_check",
		          type:"post",
		          data:{menuid: menuid},
		          success:function(res){
		          console.log(res);
		          if(res.code==100){
		          show_validate_msg("#menu_id_input", "success", "权限编号可用");
		          }else{
		          show_validate_msg("#menu_id_input", "error", "权限编号不可用");
		          }
		          
		          }
		
		})
	});
//校验结果提示信息
	function show_validate_msg(ele, status, msg) {
		//清除当前元素的校验状态
		$(ele).next("span").text("");
		if ("success" == status) {
			$(ele).next("span").text(msg);
		} else if ("error" == status) {
			$(ele).next("span").text(msg);
		}
	}    
      //添加区域信息
    function addMenu(){
    var menuid = $("#menu_id_input").val();
    var name = $("#menu_name_input").val();
    var url = $("#url_input").val();
    var treeObj = $.fn.zTree.getZTreeObj("zTree");
    var nodes = treeObj.getCheckedNodes(true);
    
    if(nodes.length == 0 ){
		alert("请选择上级权限!")
		}else{
		var pid = "";
		$.each(nodes,function(i,node){
		   pid+=node.menuid;
		});
		console.log(pid);
		$.ajax({
		     type:"post",
             url:"<%=path%>/menu/menu_add_action",		  
 		    data:{menuid: menuid,
		              name:name,
		              url:url,
		              pid:pid
		              },
 		    type:"post",
		    traditional:true,
		    success:function(res){
 		    if(res.code==100){
 		              alert("添加成功");
 		              }else{
 		               alert("添加失败");
		              }
		    }
		});
		}
		
    }
     
</script>

</body>
</html>
